package com.example.service.impl;

import com.example.entity.JsMind;
import com.example.entity.Menu;
import com.example.mapper.JsMindMapper;
import com.example.mapper.MenuMapper;
import com.example.service.JSMindService;
import com.example.utils.File.PdfUtil;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.SimpleBookmark;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Service
public class JsMindServiceImpl implements JSMindService {
    @Resource
    JsMindMapper jsMindMapper;
    @Resource
    MenuMapper menuMapper;

    @Override
    public List<Menu> BookMenu(String book_name) {

        return jsMindMapper.GetBookMenu(book_name);
    }

    @Override
    public JsMind CreateMind(String book_name) {
        JsMind jsMind = new JsMind();
        //获取元数据
        JsMind.Meta meta = jsMind.new Meta();
        meta.setName(book_name);
        String teacher_num = null;
        if((teacher_num=jsMindMapper.GetTeacherNum(book_name))==null){
          meta.setAuthor("暂无教师授课");
        }else meta.setAuthor(jsMindMapper.GetTeacherName(teacher_num));

        meta.setVersion("1.0");
        jsMind.setMeta(meta);
        //获取数据内容
        List<Menu> menuList = jsMindMapper.GetBookMenu(book_name);
        List<JsMind.DataContent> dataContentList = new ArrayList<>();
        JsMind.DataContent root = jsMind.new DataContent();
        root.setId(book_name);
        root.setIsroot(true);
        root.setTopic(book_name);
        dataContentList.add(root);
        for(Menu menu:menuList){
            JsMind.DataContent data = jsMind.new DataContent();
            if(menu.getDirection()!=null){
                data.setExpanded(false);
            }
            data.setId(menu.getId());
            data.setTopic(menu.getTopic());
            data.setDirection(menu.getDirection());
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("hasContent",menu.getHas_content());
            data.setData(hashMap);
            data.setParentid(menu.getParent_id());
            dataContentList.add(data);
        }
        jsMind.setData(dataContentList);

//        System.out.println(jsMind);
        return jsMind;
    }


    @Override
    public Boolean AddBook(MultipartFile file) throws IOException {

        List<Menu> menuList = new ArrayList<>();
        PdfUtil pdfUtil = new PdfUtil();
        int flag = 1;
        int flag2 = 1;
        PdfReader reader = new PdfReader(file.getBytes());
        List list = SimpleBookmark.getBookmark(reader);
        for (Iterator i = list.iterator(); i.hasNext(); ) {
            pdfUtil.showBookmark((Map) i.next());
        }
        String file_name=file.getOriginalFilename();
        String book_name= file_name.substring(0,file_name.lastIndexOf('.'));


        for (Map title: PdfUtil.TitleList){

            Menu menu = new Menu();
            menu.setBook_name(book_name);
            String data = (String) title.get("Title");
            String[] content = data.split("\\s+");
            menu.setHas_content("否");
            if(content.length>1){
                menu.setId(content[0]);
                menu.setTopic(content[1]);
            }else{
                menu.setId(content[0]+flag);
                menu.setTopic(content[0]+flag);
            }

            menu.setParent_id(menu.getBook_name());
            if(flag<PdfUtil.TitleList.size()/2){
                menu.setDirection("left");
            }else menu.setDirection("right");

            flag++;
            menuList.add(menu);
            //            System.out.println(title.get("Kids"));
            for(Map l : (List<Map>)title.get("Kids")){
                Menu menu2 = new Menu();
                menu2.setBook_name(book_name);
                String data1 = (String) l.get("Title");
                String[] content1 = data1.split("\\s+");
                menu2.setHas_content("否");
                if(content1.length>1){
                    menu2.setId(content1[0]);
                    menu2.setTopic(content1[1]);
                }else{
                    menu2.setId(content1[0]+flag2);
                    menu2.setTopic(content1[0]+flag2);
                }
                flag2++;
                menu2.setParent_id(content[0]);
                menuList.add(menu2);
            }

        }
        reader.close();
        return (jsMindMapper.AddBook(menuList.get(0).getBook_name()) !=0 ) && (menuMapper.AddMenu(menuList) !=0);
    }

    @Override
    public Integer updateContent(String book_name, String menu_topic) {
        return jsMindMapper.updateContent(book_name,menu_topic);
    }

    @Override
    public Boolean IfBookExist(String book_name) {
        return jsMindMapper.FindBookName(book_name).size()!=0;
    }
}
